package com.mxsat.common.enums;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: EnumDictionaryCollection
 * @Description: 动态数据字典集合
 * @author 江付祥（json.z@foxmail.com）
 * @date 2017年5月18日 下午3:28:39
 *
 */
public class EnumDictionaryCollection implements DictionaryCollection {
	private static final long serialVersionUID = 4849230041356031770L;
	private static final String ValuesMethodName = "values";
	private List<DictionaryItem> items = null;
	private Class<? extends EnumDictionaryItem<?>> clz;

	private String dictionaryName;
	private String dictionaryCode;

	public EnumDictionaryCollection(Class<? extends EnumDictionaryItem<?>> clz) {
		this.clz = clz;
		this.init();
	}

	@Override
	public String getDictionaryName() {
		return this.dictionaryName;
	}

	@Override
	public String getDictionaryCode() {
		return this.dictionaryCode;
	}

	@Override
	public List<? extends DictionaryItem> getDictionaryItems() {
		return this.items;
	}

	private void init() {
		try {
			Method method = clz.getMethod(ValuesMethodName);
			EnumDictionaryItem<?>[] arr = (EnumDictionaryItem<?>[]) method.invoke(null);
			this.items = new ArrayList<DictionaryItem>();
			for (EnumDictionaryItem<?> item : arr) {
				this.items.add(item);
			}
			if (arr.length > 0) {
				dictionaryName = arr[0].getEnumName();
				dictionaryCode = arr[0].getEnumCode();
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		 
	}
}
